<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
    <input type="text" placeholder="请输入数字1" id="i1"><br>
    <input type="text" placeholder="请输入数字2" id="i2"><br>
    <button onclick="calc('+')">加</button>
    <button onclick="calc('-')">减</button>
    <button onclick="calc('*')">乘</button>
    <button onclick="calc('/')">除</button>
    <h4>结果：<span></span></h4>
    <script>
        function calc(o){
            // 获取用户输入的两个数字
            let i1 = document.querySelector('#i1').value;
            let i2 = document.querySelector('#i2').value;
            i1=parseFloat(i1);
            i2=parseFloat(i2);
            //根据用来显示结果的span元素
            let spanE=document.querySelector('span')
            //根据不同的运算符进行运算
            switch (o){
                case '+':
                    spanE.innerHTML=i1+i2;break;
                case '-':
                    spanE.innerHTML=i1-i2;break;
                case '*':
                    spanE.innerHTML=i1*i2;break;
                case '/':
                    if (i2!=0){
                        spanE.innerHTML=i1/i2;
                    }else {
                        alert("除数不能为0");
                    }

            }
        }
    </script>
</body>
</html>